package com.cn.onetrip.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.onetrip.adapter.RouteItemAdapter;
import com.cn.onetrip.objects.AdvertsObj;
import com.cn.onetrip.objects.ArticleListObj;
import com.cn.onetrip.objects.VisitsListObj;
import com.cn.onetrip.objects.WeatherObj;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.ImageUtils;
import com.cn.onetrip.untility.NetworkManager;
import com.cn.onetrip.untility.SyncImageLoader;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.SyncImageLoader.OnImageLoadListener;
import com.cn.onetrip.view.TrapezoidImageButton;
import com.umeng.analytics.MobclickAgent;

public class TripActivity extends Activity {

	private final String mPageName = "TripActivity";
	// private View view;
	private TrapezoidImageButton photo;
	private RelativeLayout route, downLayout;
	private ImageView video, info;
	private ImageButton returnBtn, expense;
	private ImageView imageRoute, weather_icon;
	private TextView titletTextView, weathertTextView;
	private SyncImageLoader syncImageLoader;

	public static boolean One_Trip = false;
	public static boolean isOfflineData = true;
	public static String scenic_slug = "sclzgc";
	public String slug = "";

	private SysApplication sysApp;

	private RelativeLayout index_page_guid;
	private ImageView index_page_one, index_page_two, index_page_three;
	private ImageView image_bg_top, image_bg_map, image_bg_mid,
			image_bg_bottom, imageBottom_bg_bottom;
	private int indexPage = 0;

	private Animation animation_0 = null;
	private Animation animation_1 = null;

	private RelativeLayout image_map_layout;
	private ListView viewRouteList;
	private RouteItemAdapter adapter;
	private List<VisitsListObj> visitsList;
	private VisitsListObj visitsListObj;
	private List<ArticleListObj> detailedSites;
	private int RouteNum = 0;
	private Animation animUp, animDown1, animDown2, animDown3;
	private boolean isOpen = false;
	private boolean isClick = false;

	// private ImageView imageBottom;
	private RelativeLayout linearLayoutBottom, linearLayoutMid;
	public static Bitmap bmp;

	// private TextView map_OtherDesc
	private TextView map_OtherCount;
	private LinearLayout imgDescLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trip_main);
		if (SysApplication.visitsListObj.size() == 0
				|| SysApplication.routesList.size() == 0) {
			finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		slug = scenic_slug;

		index_page_guid = (RelativeLayout) findViewById(R.id.index_page_guid);

		index_page_one = (ImageView) findViewById(R.id.index_page_one);
		index_page_two = (ImageView) findViewById(R.id.index_page_two);
		index_page_three = (ImageView) findViewById(R.id.index_page_three);

		image_bg_top = (ImageView) findViewById(R.id.image_bg_top);
		image_bg_map = (ImageView) findViewById(R.id.image_bg_map);
		image_bg_mid = (ImageView) findViewById(R.id.image_bg_mid);
		image_bg_bottom = (ImageView) findViewById(R.id.image_bg_bottom);
		imageBottom_bg_bottom = (ImageView) findViewById(R.id.imageBottom_bg_bottom);

		weathertTextView = (TextView) findViewById(R.id.tripTitleWeather);
		imageRoute = (ImageView) findViewById(R.id.tripRouteImage);
		route = (RelativeLayout) findViewById(R.id.imageBtnRoute);
		downLayout = (RelativeLayout) findViewById(R.id.downBottom);
		video = (ImageView) findViewById(R.id.imageBtnVideo);
		expense = (ImageButton) findViewById(R.id.imageBtnExpense);
		returnBtn = (ImageButton) findViewById(R.id.return_button);
		imgDescLayout = (LinearLayout) findViewById(R.id.map_Other_layout);

		sysApp = new SysApplication(getApplicationContext());
		if (!One_Trip)
			returnBtn.setVisibility(View.GONE);

		animation_0 = AnimationUtils.loadAnimation(TripActivity.this,
				R.anim.index_alpha_0);
		animation_1 = AnimationUtils.loadAnimation(TripActivity.this,
				R.anim.index_alpha_1);

		// map_OtherDesc = (TextView) findViewById(R.id.map_OtherDesc);
		map_OtherCount = (TextView) findViewById(R.id.map_OtherCount);

		List<ArticleListObj> visitList = SysApplication.visitsListObj.get(0).detailedsSites;
		String scenicName = "";
		for (int i = 0; i < visitList.size(); i++) {
			scenicName += visitList.get(i).title + "/";
		}
		if (scenicName.endsWith("/")) {
			scenicName = scenicName.substring(0, scenicName.length() - 1);
		}

		ImageView ImgTag1 = new ImageView(getApplicationContext());
		ImgTag1.setBackgroundResource(R.drawable.home_recommend1);
		ImageView ImgTag2 = new ImageView(getApplicationContext());
		ImgTag2.setBackgroundResource(R.drawable.home_recommend2);
		ImageView ImgTag3 = new ImageView(getApplicationContext());
		ImgTag3.setBackgroundResource(R.drawable.home_recommend3);
		TextView TextTag1 = new TextView(getApplicationContext());
		TextTag1.setText(visitList.get(0).title);
		TextTag1.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
				.getDimensionPixelSize(R.dimen.trip_map_other_desc));
		TextTag1.setTextColor(getResources().getColor(R.color.map_other_desc));

		TextView TextTag2 = new TextView(getApplicationContext());
		TextTag2.setText(visitList.get(1).title);
		TextTag2.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
				.getDimensionPixelSize(R.dimen.trip_map_other_desc));
		TextTag2.setTextColor(getResources().getColor(R.color.map_other_desc));

		TextView TextTag3 = new TextView(getApplicationContext());
		if (visitList.size() == 3) {
			TextTag3.setText(visitList.get(2).title);
		} else {
			TextTag3.setText(visitList.get(2).title + "...");
		}
		TextTag3.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources()
				.getDimensionPixelSize(R.dimen.trip_map_other_desc));
		TextTag3.setTextColor(getResources().getColor(R.color.map_other_desc));
		TextTag3.setEllipsize(TruncateAt.END);
		TextTag3.setSingleLine();
		imgDescLayout.addView(ImgTag1);
		imgDescLayout.addView(TextTag1);
		imgDescLayout.addView(ImgTag2);
		imgDescLayout.addView(TextTag2);
		imgDescLayout.addView(ImgTag3);
		imgDescLayout.addView(TextTag3);

		// map_OtherDesc.setText(getResources().getString(
		// R.string.tripmap_recommend_route));
		map_OtherCount.setText(getResources().getString(R.string.tripmap_gong)
				+ visitList.size()
				+ getResources().getString(R.string.tripmap_scenics));
		photo = (TrapezoidImageButton) findViewById(R.id.imageBtnPhoto);
		// travel = (TrapezoidImageButton) findViewById(R.id.imageBtnTravel);
		info = (ImageView) findViewById(R.id.imageBtnInfo);
		// more = (TrapezoidImageButton) findViewById(R.id.imageBtnMore);

		titletTextView = (TextView) findViewById(R.id.tripTitleText);
		weather_icon = (ImageView) findViewById(R.id.weather_icon);

		returnBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (sysApp != null) {
					sysApp.clearData();
				}
				returnClick();
			}
		});

		// if (getSharePrefrence()) {
		index_page_guid.setVisibility(View.GONE);
		// } else {
		// index_page_guid.setVisibility(View.VISIBLE);
		// setButtonEnable(false);
		// setIndexPage();
		// }

		visitsList = SysApplication.visitsListObj;

		linearLayoutMid = (RelativeLayout) findViewById(R.id.linearLayoutMid);
		linearLayoutBottom = (RelativeLayout) findViewById(R.id.linearLayoutBottom);
		// imageBottom = (ImageView) findViewById(R.id.imageBottom);
		image_map_layout = (RelativeLayout) findViewById(R.id.layoutTitleImage);
		viewRouteList = (ListView) findViewById(R.id.routeList);

		RouteItemAdapter.isVisiable = false;
		adapter = new RouteItemAdapter(TripActivity.this, viewRouteList,
				mHandler);
		loadDate();
		viewRouteList.setAdapter(adapter);

		index_page_guid.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				indexPage++;
				clearAnim();
				if (indexPage == 1) {
					setIndexPageTwo();
				} else if (indexPage == 2) {
					setIndexPageThree();
				} else {
					index_page_one.startAnimation(animation_0);
					index_page_three.startAnimation(animation_0);
					index_page_two.startAnimation(animation_0);
					animation_0.setAnimationListener(new AnimationListener() {

						@Override
						public void onAnimationStart(Animation arg0) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationRepeat(Animation arg0) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onAnimationEnd(Animation arg0) {
							setPageOver();
							index_page_guid.setVisibility(View.GONE);
							saveSharePrefrence();
							setButtonEnable(true);
						}
					});
				}
			}
		});
		fillPagedata();
	}

	public void loadDate() {
		visitsListObj = visitsList.get(0);
		detailedSites = visitsListObj.detailedsSites;
		int typeCount = 0;
		for (int i = 0; i < detailedSites.size(); i++) {
			ArticleListObj articleListObj = detailedSites.get(i);
			if (articleListObj.type_cd != 0) {
				typeCount++;
			}
			adapter.addRoute(articleListObj.icon, articleListObj.title,
					articleListObj.description, articleListObj.distance,
					articleListObj.spendTime, articleListObj.type_cd);
		}
		adapter.setTypeCount(typeCount);
		adapter.notifyDataSetChanged();
	}

	private void setButtonEnable(boolean flag) {
		imageRoute.setEnabled(flag);
		route.setEnabled(flag);
		video.setEnabled(flag);
		expense.setEnabled(flag);
		photo.setEnabled(flag);
		info.setEnabled(flag);
		returnBtn.setEnabled(flag);
	}

	private void clearAnim() {
		index_page_one.clearAnimation();
		index_page_two.clearAnimation();
		index_page_three.clearAnimation();
	}

	private void setPageOne() {
		image_bg_top.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_map.setBackgroundColor(Color.argb(0, 0, 0, 0));
		image_bg_mid.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_bottom.setBackgroundColor(Color.argb(220, 0, 0, 0));
		imageBottom_bg_bottom.setBackgroundColor(Color.argb(220, 0, 0, 0));
	}

	private void setPageTwo() {
		image_bg_top.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_map.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_mid.setBackgroundColor(Color.argb(0, 0, 0, 0));
		image_bg_bottom.setBackgroundColor(Color.argb(220, 0, 0, 0));
		imageBottom_bg_bottom.setBackgroundColor(Color.argb(210, 0, 0, 0));
	}

	private void setPageThree() {
		image_bg_top.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_mid.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_map.setBackgroundColor(Color.argb(220, 0, 0, 0));
		image_bg_bottom.setBackgroundColor(Color.argb(0, 0, 0, 0));
		imageBottom_bg_bottom.setBackgroundColor(Color.argb(220, 0, 0, 0));
	}

	private void setPageOver() {
		image_bg_top.setBackgroundColor(Color.argb(0, 0, 0, 0));
		image_bg_mid.setBackgroundColor(Color.argb(0, 0, 0, 0));
		image_bg_map.setBackgroundColor(Color.argb(0, 0, 0, 0));
		image_bg_bottom.setBackgroundColor(Color.argb(0, 0, 0, 0));
		imageBottom_bg_bottom.setBackgroundColor(Color.argb(0, 0, 0, 0));
	}

	private void setIndexPage() {
		setPageOne();
		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		Drawable drawable1 = getResources().getDrawable(
				R.drawable.home_guide_1_1);
		Drawable drawable2 = getResources().getDrawable(
				R.drawable.home_guide_1_2);
		Drawable drawable3 = getResources().getDrawable(
				R.drawable.home_guide_1_3);
		int height1 = (drawable1.getIntrinsicHeight() * screenWidth)
				/ drawable1.getIntrinsicWidth();
		int height2 = (drawable2.getIntrinsicHeight() * screenWidth)
				/ drawable2.getIntrinsicWidth();
		int height3 = (drawable3.getIntrinsicHeight() * screenWidth)
				/ drawable3.getIntrinsicWidth();
		RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
				screenWidth, height1);
		RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
				screenWidth, height2);
		RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
				screenWidth, height3);
		lp3.setMargins(0, 0, 0, 10);
		lp3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lp2.addRule(RelativeLayout.BELOW, R.id.index_page_one);
		index_page_one.setBackgroundDrawable(drawable1);
		index_page_two.setBackgroundDrawable(drawable2);
		index_page_three.setBackgroundDrawable(drawable3);
		index_page_one.setLayoutParams(lp1);
		index_page_two.setLayoutParams(lp2);
		index_page_three.setLayoutParams(lp3);
	}

	private void setIndexPageTwo() {
		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		final Drawable drawable1 = getResources().getDrawable(
				R.drawable.home_guide_2_1);
		final Drawable drawable2 = getResources().getDrawable(
				R.drawable.home_guide_2_2);
		int height1 = (drawable1.getIntrinsicHeight() * screenWidth)
				/ drawable1.getIntrinsicWidth();
		int height2 = (drawable2.getIntrinsicHeight() * screenWidth)
				/ drawable2.getIntrinsicWidth();
		final RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
				screenWidth, height1);
		final RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
				screenWidth, height2);
		lp2.setMargins(0, 0, 0, 10);
		lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

		setPageTwo();
		index_page_one.startAnimation(animation_0);
		index_page_three.startAnimation(animation_0);
		animation_0.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				clearDrable();
				clearAnim();

				index_page_one.setBackgroundDrawable(drawable1);
				index_page_three.setBackgroundDrawable(drawable2);
				index_page_one.setLayoutParams(lp1);
				index_page_three.setLayoutParams(lp2);

				index_page_one.startAnimation(animation_1);
				index_page_three.startAnimation(animation_1);
			}
		});

	}

	private void setIndexPageThree() {
		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		final Drawable drawable1 = getResources().getDrawable(
				R.drawable.home_guide_3_1);
		final Drawable drawable2 = getResources().getDrawable(
				R.drawable.home_guide_3_2);
		final Drawable drawable3 = getResources().getDrawable(
				R.drawable.home_guide_3_3);
		int height1 = (drawable1.getIntrinsicHeight() * screenWidth)
				/ drawable1.getIntrinsicWidth();
		int height2 = (drawable2.getIntrinsicHeight() * screenWidth)
				/ drawable2.getIntrinsicWidth();
		int height3 = (drawable3.getIntrinsicHeight() * screenWidth)
				/ drawable3.getIntrinsicWidth();
		final RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
				screenWidth, height1);
		final RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
				screenWidth, height2);
		final RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
				screenWidth, height3);
		lp3.setMargins(0, 0, 0, 10);
		lp3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		lp2.setMargins(0, 30, 0, 0);
		lp2.addRule(RelativeLayout.BELOW, R.id.index_page_one);

		setPageThree();
		index_page_one.startAnimation(animation_0);
		index_page_two.startAnimation(animation_0);
		index_page_three.startAnimation(animation_0);
		animation_0.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				clearDrable();
				clearAnim();

				index_page_one.setBackgroundDrawable(drawable1);
				index_page_two.setBackgroundDrawable(drawable2);
				index_page_three.setBackgroundDrawable(drawable3);
				index_page_one.setLayoutParams(lp1);
				index_page_two.setLayoutParams(lp2);
				index_page_three.setLayoutParams(lp3);
				index_page_one.startAnimation(animation_1);
				index_page_two.startAnimation(animation_1);
				index_page_three.startAnimation(animation_1);
			}
		});

	}

	private void clearDrable() {
		if (index_page_one != null) {
			index_page_one.setBackgroundDrawable(null);
		}
		if (index_page_two != null) {
			index_page_two.setBackgroundDrawable(null);
		}
		if (index_page_three != null) {
			index_page_three.setBackgroundDrawable(null);
		}
	}

	private void saveSharePrefrence() {
		SharedPreferences remindShared = getSharedPreferences("index", 0);
		remindShared.edit().putBoolean("indexPage", true).commit();
	}

	private Boolean getSharePrefrence() {
		SharedPreferences remindShared = getSharedPreferences("index", 0);
		boolean flag = remindShared.getBoolean("indexPage", false);
		return flag;

	}

	@Override
	protected void onResume() {
		super.onResume();
		isClick = false;
		isOpen = false;
		image_map_layout.clearAnimation();
		downLayout.clearAnimation();
		linearLayoutBottom.clearAnimation();
		linearLayoutMid.clearAnimation();
		setButtonEnable(true);
		MobclickAgent.onPageStart(mPageName);
		MobclickAgent.onResume(TripActivity.this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		scenic_slug = "";
		MobclickAgent.onPageEnd(mPageName);
		MobclickAgent.onPause(TripActivity.this);
	}

	private void fillPagedata() {
		Message msg = Message.obtain();
		msg.what = 0;
		mHandler.sendMessage(msg);
	}

	private void setWeatherData() {
		if (!NetworkManager.CheckWifiState(getApplicationContext())) {
			return;
		}
		WeatherObj weatherObj = SysApplication.weatherObj;
		if (weatherObj != null) {
			weather_icon.setVisibility(View.VISIBLE);
			weathertTextView.setVisibility(View.VISIBLE);
			int drawableID = getResources().getIdentifier(
					"a_" + weatherObj.tmp_pic_from, "drawable",
					getApplicationContext().getPackageName());
			weather_icon.setBackgroundResource(drawableID);
			weathertTextView.setText("      " + weatherObj.tmp_current + "\n"
					+ weatherObj.tmp_wind);
		}
	}

	private void setVideoData() {
		if (SysApplication.videoList.size() == 0) {
			// video.setVisibility(View.GONE);
			// ((ImageView) findViewById(R.id.image_split))
			// .setVisibility(View.GONE);
			// route.setBackgroundResource(R.drawable.home5_selector);
		} else if (SysApplication.videoList.size() == 1) {
			video.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					video.setBackgroundResource(R.drawable.home8_gray);
				}
			});
			// video.setVisibility(View.GONE);
			// ((ImageView) findViewById(R.id.image_split))
			// .setVisibility(View.GONE);
			// route.setBackgroundResource(R.drawable.home5_selector);
		} else if (SysApplication.videoList.size() > 2) {
			// video.setBackgroundResource(R.drawable.home6_selector);
			video.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (SysApplication.contentObj.isFree == false) {
						Message msg = Message.obtain();
						msg.what = 9;
						mHandler.sendMessage(msg);
						return;
					}
					Intent intent = new Intent(getApplicationContext(),
							VideosActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_right_translate,
							R.anim.pull_left_translate);
				}
			});
		} else {
			video.setBackgroundResource(R.drawable.home8_selector);
			video.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (SysApplication.contentObj.isFree == false) {
						Message msg = Message.obtain();
						msg.what = 9;
						mHandler.sendMessage(msg);
						return;
					}
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					intent.setClass(getApplicationContext(),
							ScenicVideoPlayActivity.class);
					bundle.putString("videoUrl",
							SysApplication.videoList.get(1).mediaUrl);
					intent.putExtras(bundle);

					// intent.setClass(getApplicationContext(),
					// VideosGridActivity.class);

					startActivity(intent);
					overridePendingTransition(R.anim.push_right_translate,
							R.anim.pull_left_translate);
				}
			});

			// String timeVideo =
			// formateVideoTime(SysApplication.videoList.get(1).duration);
			// ((TextView) findViewById(R.id.textVideo)).setText(timeVideo);
		}

		// if (SysApplication.videoList.size() > 0) {
		// String timeRoute =
		// formateVideoTime(SysApplication.videoList.get(0).duration);
		// ((TextView) findViewById(R.id.textRoute)).setText(timeRoute);
		// }
	}

	private String formateVideoTime(int duration) {
		String timeString = String.format("\n%02d:%02d", duration / 60,
				duration % 60);

		return timeString;
	}

	private void setImageRouteData() {
		imageRoute.setBackgroundResource(R.drawable.default_image_big);
		syncImageLoader = new SyncImageLoader(TripActivity.this);
		syncImageLoader.loadImage(SysApplication.contentObj.guide_icon,
				new OnImageLoadListener() {

					@Override
					public void onImageLoad(Drawable drawable) {
						if (drawable != null) {
							int height = (drawable.getIntrinsicHeight() * imageRoute
									.getWidth()) / drawable.getIntrinsicWidth();
							RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
									LayoutParams.MATCH_PARENT, height);
							// lp.addRule(RelativeLayout.BELOW,
							// R.id.layoutTitle);
							lp.setMargins(0, downLayout.getTop() - height, 0, 0);
							Log.i("downLayout.getTop()==where",
									downLayout.getTop() + "=="
											+ (downLayout.getTop() - height));
							imageRoute.setLayoutParams(lp);
							imageRoute.setBackgroundDrawable(drawable);
							// ((ImageView) findViewById(R.id.image_route_text))
							// .setVisibility(View.VISIBLE);
							((ImageView) findViewById(R.id.map_click))
									.setVisibility(View.VISIBLE);
						} else {
							imageRoute
									.setBackgroundResource(R.drawable.default_image_big);
						}
					}

					@Override
					public void onImageLoad(Integer t, Drawable drawable) {
					}

					@Override
					public void onError() {
					}

					@Override
					public void onError(Integer t) {
					}
				});
	}

	private void addEventListener() {

		imageRoute.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (isClick) {
					return;
				}
				isClick = true;
				animUp = AnimationUtils.loadAnimation(getApplicationContext(),
						R.anim.translation_up);
				animDown1 = AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.translation_down);
				animUp.setFillAfter(true);
				animDown1.setFillAfter(true);

				image_map_layout.startAnimation(animUp);
				downLayout.startAnimation(animDown1);
				animUp.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
						setButtonEnable(false);
					}

					@Override
					public void onAnimationRepeat(Animation animation) {

					}

					@Override
					public void onAnimationEnd(Animation arg0) {
						if (!isOpen) {
							isOpen = true;

							Intent intent = new Intent(getApplicationContext(),
									RouteActivity.class);
							startActivity(intent);

							// View view = getWindow().getDecorView();
							// bmp = ImageUtils.getViewBitmap(view);

							overridePendingTransition(R.anim.translation1,
									R.anim.translation1);

							// Message msg = Message.obtain();
							// msg.what = 0;
							// mHandler.sendMessage(msg);
						}
					}
				});

				// Intent intent = new Intent(getApplicationContext(),
				// RouteActivity.class);
				// startActivity(intent);
				// overridePendingTransition(R.anim.push_right_translate,
				// R.anim.pull_left_translate);

			}
		});
		route.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (SysApplication.videoList.size() <= 0) {
					return;
				}
				if (SysApplication.contentObj.isFree == false) {
					Message msg = Message.obtain();
					msg.what = 9;
					mHandler.sendMessage(msg);
					return;
				}
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				intent.setClass(getApplicationContext(),
						ScenicVideoPlayActivity.class);
				bundle.putString("videoUrl",
						SysApplication.videoList.get(0).mediaUrl);
				intent.putExtras(bundle);

				startActivity(intent);
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});
		photo.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (v.onTouchEvent(event)
						&& event.getAction() == MotionEvent.ACTION_UP
						&& v.isPressed()) {
					Intent intent = new Intent(getApplicationContext(),
							ScenicAtlasActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_right_translate,
							R.anim.pull_left_translate);
				}
				return false;
			}
		});
		// travel.setOnTouchListener(new OnTouchListener() {
		//
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// if (v.onTouchEvent(event)
		// && event.getAction() == MotionEvent.ACTION_UP
		// && v.isPressed()) {
		// Intent intent = new Intent(getApplicationContext(),
		// TravelActivity.class);
		// startActivity(intent);
		// overridePendingTransition(R.anim.push_right_translate,
		// R.anim.pull_left_translate);
		// }
		// return false;
		// }
		// });
		expense.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						PlayViewPagerActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});

		info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplicationContext(),
						InfoGridActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});
		// info.setOnTouchListener(new OnTouchListener() {
		//
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// if (v.onTouchEvent(event)
		// && event.getAction() == MotionEvent.ACTION_UP
		// && v.isPressed()) {
		// Intent intent = new Intent(getApplicationContext(),
		// InfoGridActivity.class);
		// startActivity(intent);
		// overridePendingTransition(R.anim.push_right_translate,
		// R.anim.pull_left_translate);
		// }
		// return false;
		// }
		// });
		// more.setOnTouchListener(new OnTouchListener() {
		//
		// @Override
		// public boolean onTouch(View v, MotionEvent event) {
		// if (v.onTouchEvent(event)
		// && event.getAction() == MotionEvent.ACTION_UP
		// && v.isPressed()) {
		// Intent intent = new Intent(getApplicationContext(),
		// MoreActivity.class);
		// startActivity(intent);
		// overridePendingTransition(R.anim.push_right_translate,
		// R.anim.pull_left_translate);
		// }
		// return false;
		// }
		// });
	}

	public Handler mHandler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {

			if (msg.what == 0) {
				titletTextView.setText(SysApplication.contentObj.title);
				setWeatherData();
				setVideoData();
//				setImageRouteData();
				addEventListener();
			} else if (msg.what == 1) {
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.network_fail),
						Toast.LENGTH_SHORT).show();
			} else if (msg.what == 2) {
			} else if (msg.what == 9) {
				NetworkManager.ChargeMessage(TripActivity.this);
			}
		};
	};

	private void getAdsImageCache() {
		syncImageLoader = new SyncImageLoader(this);
		List<AdvertsObj> advertsList = SysApplication.advertsList;
		for (int i = 0; i < advertsList.size(); i++) {
			final AdvertsObj advertsObj = advertsList.get(i);
			syncImageLoader.loadImage(advertsObj.advert_icon,
					new OnImageLoadListener() {

						@Override
						public void onImageLoad(Drawable drawable) {
							if (drawable != null) {
								SysApplication.adCache.put(
										advertsObj.advert_icon, drawable);
							}
						}

						@Override
						public void onImageLoad(Integer t, Drawable drawable) {
						}

						@Override
						public void onError() {
						}

						@Override
						public void onError(Integer t) {
						}
					});
		}
	}

	@Override
	protected void onDestroy() {
		if (sysApp != null) {
			sysApp.clearData();
		}
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (isClick) {
				return true;
			}
			if (One_Trip) {
				if (sysApp != null) {
					sysApp.clearData();
				}
				returnClick();
			} else {
				CloseApp();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private void CloseApp() {
		AlertDialog.Builder builder = new AlertDialog.Builder(TripActivity.this);
		builder.setTitle(R.string.app_map_name);
		builder.setMessage(R.string.quit_message);
		builder.setPositiveButton(R.string.quit_confirm,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
						overridePendingTransition(R.anim.push_left_translate,
								R.anim.pull_right_translate);
						dialog.dismiss();
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});
		builder.setNegativeButton(R.string.cancel,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		builder.create();
		builder.show();
	}

	private void returnClick() {
		System.gc();
		finish();
		overridePendingTransition(R.anim.push_left_translate,
				R.anim.pull_right_translate);

		if (IndexLoadingActivity.isOfflineData) {
			android.os.Process.killProcess(android.os.Process.myPid());
			java.lang.System.exit(0);
		}
	}
}
